{include file='public/meta' }
  <title>栏目管理 - 木鱼内容管理系统</title>
  <style>
  .form1 {
      display: flex;
  }
  </style>
</head>
<body>
<style type="text/css" media="all">
        .pmenu-c{background:#e9e8e8}
        .layui-table .ew-tree-table-indent {margin-right: 5px;padding-left: 16px;}
        .layui-table tr td i{margin-right: 5px;}
    </style>
  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
		 
		 
		<div class="form1">
			<div class="layui-form-item">
			    <label class="layui-form-label" style="width: 200px;">栏目迁移[<span style="color: red;">*不支持顶级栏目迁移</span>]</label>
			    <div class="layui-input-inline">
			        <div id="lmqy">
						
					</div>
			    </div>
			</div>
			<span style="font-size: 25px;padding-right: 10px;">→</span>
			<div class="layui-form-item">
			    <div class="layui-input-inline">
			        <div id="lmqy1">
			        	
			        </div>
			    </div>
			</div>
			<div class="form1">
			    <button type="submit" onclick="movecate();" class="layui-btn" style="margin-left:40%;">确定</button>
			</div>
		</div> 
		 
		  
		<div class="layui-table-body layui-table-main">
			
			<table class="layui-table" id="mattertable">
					    
			</table>
		</div>
			
      </div>
    </div>
  </div>

  {include file='public/footer' }
  {load href="__ADMINST__muyuadmin/xm-select.js"}
    <script type="text/html" id="rightbar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
	<script type="text/html" id="toptool">
	<a class="layui-btn layui-btn-danger layuiadmin-btn-useradmin" lay-event="delall">批量删除</a>
	<a class="layui-btn layuiadmin-btn-useradmin" onclick="openwind('添加栏目','{:url('Category/add')}')">添加</a>
	</script>
  <script>
  layui.config({
    base: '__ADMINST__layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' ,//主入口模块
  }).use(['treeTable','index', 'useradmin'], function(){
      
    var $ = layui.$
    ,form = layui.form
    ,treeTable = layui.treeTable;
    
    
	var insTb = treeTable.render({
            elem: '#mattertable',
            data: {$menu|raw},
			toolbar: '#toptool',
			defaultToolbar:[],
            tree: {
                iconIndex: 2,
                isPidData: true,
                idName: 'id',
                pidName: 'pid'
            },
            cols: [
                { type: 'checkbox', fixed: 'left' }
                ,{ field: 'id', title: 'ID', width: 20, fixed: 'left', unresize: true}
                ,{ field: 'p_title', width: 150, title: '栏目所属'}
                ,{ field: 'title', width: 150, title: '栏目名称'}
                ,{ field: 'ico', title: '栏目ico'}
                ,{ field: 'titlepic', title: '栏目背景'}
                ,{ field: 'comment', title: '栏目描述', unresize: true }
                ,{ field: 'href',title: '栏目地址'}
                ,{ field: 'sort',title: '栏目排序', width: 50, sort: true}
                ,{ field: 'status',title: '栏目状态', templet:function(st){
                    if(st.status == 1){
                        return '<button type="button" onClick="setStatus('+st.id+')" class="layui-btn-xs layui-btn">已启用</button>';
                    }else if(st.status == 0){
                        return '<button type="button" onClick="setStatus('+st.id+')" class="layui-btn-xs layui-btn layui-btn-danger">已隐藏</button>';
                    }else{
                        return '<button type="button" class="layui-btn-xs layui-btn">未知</button>';
                    }
                }}
                ,{ fixed: 'right', title: '操作', toolbar: '#rightbar'}
            ],
            style: 'margin-top:0;'
        });
	treeTable.on('tool(mattertable)', function (obj) {
            var event = obj.event;
            var data = obj.data;
            if (event === 'del') {
                layer.msg('确认删除栏目[-'+data.title+'-]吗?',{
			    time:5000,
			    shade:0.3,
			    btn:['删除','取消'],
			    yes:function() {
				// 执行删除
				$.post("{:url('Category/deletes')}",{id:data.id},function(data){
					layer.msg(data.msg,{time:2000},function(){
				    location.reload();
					});
				});
			}
		    });
            } else if (event === 'edit') {
            layer.open({
            type:2,
            title:"栏目编辑",
            shade:0.3,
            area:['50%','85%'],
            content:"{:url('Category/edit')}"+"?id="+data.id,
            });
            }
    });
	// 头部工具栏点击事件
	treeTable.on('toolbar(mattertable)', function (obj) {
		var event = obj.event;
		var checkid = insTb.checkStatus();
	    if(event === "delall"){
			if(checkid.length == 0){
				layer.msg("未选择数据",{time:2000});
			}else{
				layer.confirm("确定要删除选中的所有数据", function (index) {
				    //把所有选中的数据的id封装到一个数组中
				    var ids = new Array(checkid.length);
				    for (var i = 0; i < ids.length; i++) {
				        ids[i] = checkid[i].id;
				    }
				    //执行删除操作
				    $.post("{:url('Category/deletes')}",{id:ids},function(data){
				    if(data.code === 1){
				    	layer.msg(data.msg,{time:2000},function(){
				    		location.reload();
				    	});
				    }else{
				    	layer.msg(data.msg,{time:2000});
				    }
				    })                
			});
		}
		}
	});	
  });
  
	var lmqy,lmqy1;
	lmqy = xmSelect.render({
		el: '#lmqy', 
		toolbar:{
				show: true,
			},
			filterable: true,
			height: '500px',
			data: 
				{$qylanmu|raw}
	})
	lmqy1 = xmSelect.render({
		el: '#lmqy1',
		model: { label: { type: 'text' } },
		radio: true,
		clickClose: true,
		tree: {
			show: true,
			strict: false,
			expandedKeys: [ -1 ],
		},
		height: 'auto',
		data(){
			return {$qylanmus|raw}
			}
	})
	
	/* 
		触发迁移
	 */
	function movecate()
	{
		var cate,tocate,info;
		cate = lmqy.getValue();
		tocate = lmqy1.getValue();
		if(cate == "" || cate == null || cate == undefined){
			layer.msg('请选择需要迁移的栏目');
		}else if(tocate == "" || tocate == null || tocate == undefined){
			layer.msg('请选择目标栏目');
		}else{
		info = {id:lmqy.getValue('valueStr'),toid:lmqy1.getValue('valueStr')};
		layer.confirm('是否执行当前操作？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			tomovecate(info);
		}, function(){
			// status = {status:false};
			// info = $.extend({}, info, status);
			layer.msg("取消执行",{time:2000});
		});
		
		}
	}
	function tomovecate(Data){
	$.post("{:url('admin/Category/movecate')}",{data:Data},function(data){
		if(data.code === 1){
			layer.msg(data.msg,{time:2000},function(){
				location.reload();
			});
		}else{
			layer.msg(data.msg,{time:2000});
		}
	})	
	}
    //编辑弹层
    function opentc(title,url){
        layer.open({
            type:2,
            title:title,
            shade:0.3,
            area:['50%','85%'],
            content:url,
        });
    }
	
	//状态变更
	function setStatus(id){
		layer.msg('确认要变更状态吗？',{
			time:5000,
			shade:0.3,
			btn:['确认','取消'],
			yes:function() {
				//执行变更
				$.post("{:url('Category/setStatus')}",{id:id},function(data){
					if(data.code === 1){
						layer.msg(data.msg,{time:2000},function(){
							location.reload();
						});
					}else{
						layer.msg(data.msg,{time:2000});
					}
				})
			}
		});
	}
	
	//点击搜索
	function so(){
		var keywords = $("#keywords").val();
		if(keywords == ""){
			layer.msg("请输入搜索内容！",{time:2000});
		}else{
			layer.open({
				title:"搜索内容",
				type:2,
				area:['100%','100%'],
				content: "{:url('Category/search')}"+"?keywords="+keywords,
				end:function(){
					location.reload();
				}
			});
		}
		//console.log(keywords);
	}
    
    function updatecategory(){
                $.ajax({
				    type: 'POST',
				    url: '{:url("admin/Category/updatecategory")}',
				    dataType: "json",
				    beforeSend: function() {
				        index = layer.load();
				    },
				    success: function(data) {
				        layer.msg(data.msg, {time: 2000}, function() {
				            location.reload();
				        });
				    },
				    complete: function() {
				        layer.close(index);
				    }
				});
    }
	</script>

</body>
</html>
